<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="input">

    <script>
        const inputDom = document.querySelector("#input")
        inputDom.addEventListener("input", (e) => {
            throttleCallBack(e.target.value)
            // antiShakingCallBack(e.target.value)
            // antiShaking(500, outPut)(e.target.value);
            // throttle(500, outPut)(e.target.value)
        })

        // 需要执行函数
        function outPut(value) {
            console.log(value)
        }

        // 防抖
        const antiShaking = (awiatMs, callBack) => {
            let timer;
            return function (value) {
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(() => {
                    callBack(value)
                }, awiatMs)
            }
        }
        let antiShakingCallBack = antiShaking(500, outPut)

        // 节流
        const throttle = (awiatMs, callBack) => {
            let nowTime = 0;
            return function (value) {
                let ms = new Date().getTime()
                if (ms - nowTime >= awiatMs) {
                    callBack(value)
                    nowTime = ms;
                }
            }
        }
        let throttleCallBack = throttle(500, outPut)
    </script>
</body>

</html>